package com.vpen.picupload.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * @version: 1.0
 * @author: Vpen
 * @date: 2021/9/25 13:22
 * @content:
 */
@Configuration
public class RedisConfig {

    @Resource
    private ObjectMapper mapper;

    @Bean
    public RedisTemplate<String,Object> redisTemplate(@Qualifier("redissonConnectionFactory") RedisConnectionFactory factory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        /*
        * 设置连接工厂
        * */
        redisTemplate.setConnectionFactory(factory);
        /*
         * 使用Jackson2JsonRedisSerializer序列化工具
         */
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        /*
         * 指定要序列化的域Field、set、get，以及修饰符范围
         * ANY是都有，包括private、public
         */
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        /*
         * 如果注释掉enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)，
         * 那存储到redis里的数据将是没有类型的纯json，我们调用redis API获取到数据后，
         * java解析将是一个LinkHashMap类型的key-value的数据结构，
         * 我们需要使用的话就要自行解析，这样增加了编程的复杂度。
         *
         * 无指定时：
         * [{"id":72,"uuid":"c4d7fc52-4096-4c79-81ef-32cb1b87fd28","type":2}]
         * 有指定时：
         * ["java.util.ArrayList",[{"@class":"com.model.app","id":72,"uuid":"c4d7fc52-4096-4c79-81ef-32cb1b87fd28","type":2}]]
         *
         *
         * 指定序列化输入的类型，存储到redis里的数据将是有类型的json数据，类必须是非final修饰的，
         * final修饰的类，比如
         * public final class User implements Serializable{},会包异常
         */
//        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(mapper);

        // 设置键（key）的序列化采用StringRedisSerializer。
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);

        // 设置值（value）的序列化采用jackson2JsonRedisSerializer。
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }

    /**
     * 配置stringRedisTemplate序列化方式
     */
    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

}
